package com.daka.controller;

import com.daka.annotation.Login;
import com.daka.entity.Group;
import com.daka.entity.Organization;
import com.daka.entity.Volunteer;
import com.daka.enumerate.StatusCode;
import com.daka.service.GroupService;
import com.daka.service.OrganizationService;
import com.daka.vo.request.GroupPageRequestVo;
import com.daka.vo.request.GroupVolPageRequestVo;
import com.daka.vo.response.DataResponseVo;
import com.daka.vo.response.ListDataResponseVo;
import com.daka.vo.response.PageListResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;


/**
 * @author beliveli on 2016/3/31 0031.
 */
@Controller
@RequestMapping("/group")
@Slf4j
public class GroupController {

    @Resource(name = "groupService")
    private GroupService groupService;

    @Resource
    private OrganizationService organizationService;

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/list", consumes = "application/json")
    @Login
    public ListDataResponseVo<Group> list() {

        List<Group> topicList = groupService.selectAll();
        return new ListDataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), topicList);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/listByOrg", consumes = "application/json")
    @Login
    public ListDataResponseVo<Group> listByOrg(@Valid @RequestBody Organization organization, HttpSession session) {

        int orgId;
        if (organization.getOrgId() == 0) {
            orgId = Integer.parseInt(session.getAttribute("orgId").toString());
        } else {
            orgId = organization.getOrgId();
        }

        Organization organizationEntity = organizationService.selectById(orgId);

        List<Group> topicList = groupService.selectByOrg(organizationEntity.getOnePass());
        return new ListDataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), topicList);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/pageListByOrg", consumes = "application/json")
    @Login
    public DataResponseVo<PageListResponse<Group>> pageListByOrg(@Valid @RequestBody GroupPageRequestVo groupPageRequestVo, HttpSession session) {

        int orgId;
        if (groupPageRequestVo.getOrgId() == 0) {
            orgId = Integer.parseInt(session.getAttribute("orgId").toString());
        } else {
            orgId = groupPageRequestVo.getOrgId();
        }

        Organization organization = organizationService.selectById(orgId);

        List<Group> groupList = groupService.selectPageByOrg(organization.getOnePass(), groupPageRequestVo.getGroupName(), (groupPageRequestVo.getPageIndex() - 1) * 10, 10);
        int count = groupService.selectCountByOrg(organization.getOnePass(), groupPageRequestVo.getGroupName());
        PageListResponse<Group> page = new PageListResponse<>(groupList, count);
        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), page);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/volListById", consumes = "application/json")
    @Login
    public ListDataResponseVo<Volunteer> volListById(@Valid @RequestBody Group group) {

        List<Volunteer> volunteerList = groupService.selectVolById(group.getId());
        return new ListDataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), volunteerList);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/volPageListById", consumes = "application/json")
    @Login
    public DataResponseVo<PageListResponse<Volunteer>> volPageListById(@Valid @RequestBody GroupVolPageRequestVo groupVolPageRequestVo) {

        List<Volunteer> volunteerList = groupService.selectVolPageById(groupVolPageRequestVo.getGroupId(), groupVolPageRequestVo.getUserName(), (groupVolPageRequestVo.getPageIndex() - 1) * 10, 10);
        int count = groupService.selectVolCountById(groupVolPageRequestVo.getGroupId(), groupVolPageRequestVo.getUserName());
        PageListResponse<Volunteer> page = new PageListResponse<>(volunteerList, count);
        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), page);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/info", consumes = "application/json")
    @Login
    public DataResponseVo<Group> info(@Valid @RequestBody Group group) {
        Group result = groupService.selectById(group.getId());

        if (result == null) {
            return new DataResponseVo<>(StatusCode.JAVA_THROW_EXCEPTION.getStatusCode(), StatusCode.JAVA_THROW_EXCEPTION.getStatusMessage());
        }

        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), result);
    }
}
